package com.food.eat.controller;

import com.food.eat.pojo.UserAddress;
import com.food.eat.pojo.bo.AddressBo;
import com.food.eat.service.AddressService;
import com.food.eat.util.AssertUtil;
import com.food.eat.util.FoodResultJson;
import com.food.eat.util.MobileEmailUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author:RenPu
 * @Date: 2020/7/18 16:49
 * @Version: 1.0
 * @description:
 */
@Api(value = "用户收货地址接口", tags = "用户收货地址接口")
@RestController
@AllArgsConstructor
@RequestMapping("/address")
public class AddressController {


//    @Resource
    private final AddressService addressService;

    /**
     * 用户在确认订单页面前，可以对收货地址进行如下操作；
     * 1：查询地址
     * 2：编辑地址
     * 3：删除地址
     * 4：新增地址
     * <p>
     * address/list
     */

    @ApiOperation(value = "获取用户的收货地址", notes = "获取用户的收货地址", httpMethod = "POST")
    @PostMapping("/list")
    public FoodResultJson list(@RequestParam(value = "userId") String userId) {

        if (StringUtils.isBlank(userId)) {
            return FoodResultJson.errMsg("");
        }

        List<UserAddress> userAddresses = addressService.queryByUserId(userId);

        return FoodResultJson.ok(userAddresses);

    }



    @ApiOperation(value = "获取用户的收货地址222222", notes = "获取用户的收货地址22222", httpMethod = "GET")
    @PostMapping("/list222")
    public FoodResultJson list222(@RequestParam(value = "userId") String userId) {

        AssertUtil.businessInvalid(StringUtils.isBlank(userId),"参数检验失败！！！！");
        List<UserAddress> userAddresses = addressService.queryByUserId(userId);

        return FoodResultJson.ok(userAddresses);

    }



    @ApiOperation(value = "新增用户的收货地址", notes = "新增用户的收货地址", httpMethod = "POST")
    @PostMapping("/add")
    public FoodResultJson add(@RequestBody AddressBo addressBo) {


        FoodResultJson foodResultJson = checkAddress(addressBo);
        if (foodResultJson.getStatus() != 200) {
            return foodResultJson;
        }
        addressService.addUserNewAddress(addressBo);

        return FoodResultJson.ok("新增收货地址成功！！！！");

    }


    /**
     * 封装校验的信息方法
     *
     * @param addressBo
     * @return
     */
    private FoodResultJson checkAddress(AddressBo addressBo) {

        String mobile = addressBo.getMobile();
        boolean mobileIsOk = MobileEmailUtils.checkMobileIsOk(mobile);
        String receiver = addressBo.getReceiver();
        AssertUtil.businessInvalid(StringUtils.isBlank(receiver)|| receiver.length() > 12|| StringUtils.isBlank(mobile)||
                                             mobile.length() != 11||!mobileIsOk|| StringUtils.isBlank(addressBo.getCity()) ||
                                            StringUtils.isBlank(addressBo.getProvince()) || StringUtils.isBlank(addressBo.getDetail())
                                            || StringUtils.isBlank(addressBo.getDistrict()),"参数校验失败！！！！");
//        if (StringUtils.isBlank(receiver)) {
//            return FoodResultJson.errMsg("收货人不能为空！！！");
//        }
//        if (receiver.length() > 12) {
//            return FoodResultJson.errMsg("收货人的姓名不能太长！！！");
//        }

//        String mobile = addressBo.getMobile();
//        if (StringUtils.isBlank(mobile)) {
//            return FoodResultJson.errMsg("收货人的联系方式不能为空！！！");
//        }

//        if (mobile.length() != 11) {
//            return FoodResultJson.errMsg("收货人的联系方式长度输入有误！！！");
//        }
//        boolean mobileIsOk = MobileEmailUtils.checkMobileIsOk(mobile);
//        if (!mobileIsOk) {
//            return FoodResultJson.errMsg("收货人联系方式格式，输入有误！！！！");
//        }

//        String city = addressBo.getCity();
//        String province = addressBo.getProvince();
//        String detail = addressBo.getDetail();
//        String district = addressBo.getDistrict();
//
//        if (StringUtils.isBlank(addressBo.getCity()) || StringUtils.isBlank(addressBo.getProvince())
//                || StringUtils.isBlank(addressBo.getDetail()) || StringUtils.isBlank(addressBo.getDistrict())) {
//            return FoodResultJson.errMsg("收货地址不能为空！！！");
//        }

        return FoodResultJson.ok(addressBo);

    }


    @ApiOperation(value = "修改用户的收货地址", notes = "修改用户的收货地址", httpMethod = "POST")
    @PostMapping("/update")
    public FoodResultJson update(@RequestBody AddressBo addressBo) {

        if (StringUtils.isBlank(addressBo.getAddressId())) {
            return FoodResultJson.errMsg("修改地址错误：addressId不能为空");
        }


        FoodResultJson foodResultJson = checkAddress(addressBo);
        if (foodResultJson.getStatus() != 200) {

            return foodResultJson;

        }
        addressService.updateUserAddress(addressBo);

        return FoodResultJson.ok("修改收货地址成功！！！！");

    }


    @ApiOperation(value = "删除用户的收货地址", notes = "删除用户的收货地址", httpMethod = "POST")
    @PostMapping("/delete")
    public FoodResultJson delete(@RequestParam String userId, @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return FoodResultJson.errMsg("");
        }

        addressService.deleteUserAddress(userId, addressId);

        return FoodResultJson.ok("修改收货地址成功！！！！");

    }


    @ApiOperation(value = "用户设置默认收货地址", notes = "用户设置默认收货地址", httpMethod = "POST")
    @PostMapping("/setDefalut")
    public FoodResultJson setDefalut(@RequestParam String userId, @RequestParam String addressId) {

        if (StringUtils.isBlank(userId) || StringUtils.isBlank(addressId)) {
            return FoodResultJson.errMsg("");
        }

        addressService.updateUserAddressToDefault(userId, addressId);

        return FoodResultJson.ok("设置默认收货地址成功！！！！");

    }

}
